package io.zhengsh.nio.socketchannel;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.StandardSocketOptions;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;

public class SocketChannelDemo {

    public static void main(String[] args) throws IOException {
        //  创建 socketchannel
        SocketChannel socketChannel = SocketChannel.open(
                new InetSocketAddress("www.baidu.com", 80));

        // 测试 SocketChannel 是否 open 状态
        socketChannel.isOpen();
        // 测试 SocketChannel 是否已经被链接
        socketChannel.isConnected();
        // 测试 SocketChannel 是否正在链接状态
        socketChannel.isConnectionPending();
        // 校验正在进行套接字连接的 SocketChannel 是否已经完成链接
        socketChannel.finishConnect();

        SocketChannel socketChannel1 = SocketChannel.open();
        socketChannel1.connect(new InetSocketAddress("www.baidu.com", 80));


        SocketChannel socketChannel0 = SocketChannel.open(
                new InetSocketAddress("www.baidu.com", 80));
        ByteBuffer byteBuffer = ByteBuffer.allocate(16);
        socketChannel0.read(byteBuffer);
        socketChannel0.close();
        System.out.println("read over");

        SocketChannel socketChannel10 = SocketChannel.open(
                new InetSocketAddress("www.baidu.com", 80));
        socketChannel10.configureBlocking(false);
        ByteBuffer byteBuffer1 = ByteBuffer.allocate(16);
        socketChannel10.read(byteBuffer1);
        socketChannel10.close();
        System.out.println("read over");

        SocketChannel socketChannel5 = SocketChannel.open(
                new InetSocketAddress("www.baidu.com", 80));

        socketChannel5.setOption(StandardSocketOptions.SO_KEEPALIVE, Boolean.TRUE)
        .setOption(StandardSocketOptions.TCP_NODELAY, Boolean.TRUE);

        socketChannel5.getOption(StandardSocketOptions.SO_KEEPALIVE);
        socketChannel5.getOption(StandardSocketOptions.SO_RCVBUF);

    }
}
